/*#include <sys/mman.h>*/
#include <string.h>
#include <stdlib.h>

#define MEM_SEGMENT_SIZE 5000
#define MEM_SEGMENT_HEADER_SIZE 2*sizeof(void*)
#define NEXT sizeof(void*)

#define SEGMENT_HEADER_SIZE 2*sizeof(void*)+sizeof(int)+sizeof(char)
#define FREE sizeof(int)
#define NEXT_FREE sizeof(int)+sizeof(char)
#define PREV_FREE sizeof(int)+sizeof(char)+sizeof(void*)

#define SEGMENT_FOOTER_SIZE sizeof(int)


/* hold a list of memory segments alloced by us */
void* memHead = NULL;


/* creates a new memory segment, initializes it, allocates inside it the number of butes specified and returns it's address */
void* create_mem_segment (size_t size) {
	void *add;
	char one = 1, zero = 0;
	void *null = NULL;
	int freeSize, second;
	add = mmap(NULL, size, );
	if (add == NULL) return NULL;
		
	/* fill the header of the first segment inside it - allocate */
	/* fill the segment's size */
	memcpy (memHead + MEM_SEGMENT_HEADER_SIZE, &(size), sizeof(int));
	/* the segment isn't free */
	memcpy (memHead+FREE, &(zero), sizeof(char));
	/* prev_free and next_free are NULL - not relevent for an allocated segment */
	memcpy (memHead + NEXT_FREE, &(null), sizeof(void*));
	memcpy (memHead + PREV_FREE, &(null), sizeof(void*));
	/* fill the footer of the segment */
	memcpy (memHead + MEM_SEGMENT_HEADER_SIZE + SEGMENT_HEADER_SIZE + size, &(size), sizeof(int));

	/* fill the header of the second free segment inside it - all the rest */
	second = MEM_SEGMENT_HEADER_SIZE + SEGMENT_HEADER_SIZE + size + SEGMENT_FOOTER_SIZE;
	/* fill the segment's size */
	freeSize = MEM_SEGMENT_SIZE - MEM_SEGMENT_HEADER_SIZE - SEGMENT_HEADER_SIZE - size;
	memcpy (&(memHead + second), &(freeSize), sizeof(int));
	/* the segment is free */
	memcpy (&(memHead + second + FREE), &(one), sizeof(char));
	/* prev_free and next_free are NULL */
	memcpy (&(memHead + second + NEXT_FREE), &(null), sizeof(void*));
	memcpy (&(memHead + second + PREV_FREE), &(null), sizeof(void*));
	/* fill the footer of the segment */
	memcpy (&(memHead + second + SEGMENT_HEADER_SIZE + size), &(freeSize), sizeof(int));

	/* fill the header of the memory segment */
	/* first free address is the addrss of the second segment */
	memcpy (&(memHead), &(memHead + second), sizeof(void*));
	/* the next memory segment is NULL */
	memcpy (&(memHead + NEXT), &(null), siSEGMENT_FOOTER_SIZEzeof(void*));
		
	return add;
}

void* my_malloc (size_t size) {
	size_t memSize;
	if (memHead == NULL) {
		if (size > MEM_SEGMENT_SIZE) {
			/* size is so big that we will allocate a mem segment only for it */
			memSize = size + MEM_SEGMENT_HEADER_SIZE + SEGMENT_HEADER_SIZE + SEGMENT_FOOTER_SIZE;
			memHead = create_mem_segment(size);
		}
		else memHead = create_mem_segment(MEM_SEGMENT_SIZE);
		if (memHead == NULL) fprintf(stderr, "Error: mmap failed"); /* ??? */
		return memHead + MEM_SEGMENT_HEADER_SIZE + SEGMENT_HEADER_SIZE;
	}
	return find_free_space(size);
}

/* finds and allocates a free segment (at the smallest size possible that is larger then size) and returns it's address */
void* find_free_space (size_t size) {
	void *add;
	int blockSize;
	/* get a ptr to the address of the first free space (the smallest) */
	add = memHead + MEM_SEGMENT_HEADER_SIZE; 
	while (add != NULL) {
		memcpy(&(blockSize), add, sizeof(int));
		if (blockSize >= size) break;
		/* move to the next free space */
		memcpy(&(add), add + NEXT_FREE, sizeof(void*)); /* ??? */  
	}
	
	
}

void my_free (void* ptr) {

}

/*void make_null (void *ptr) {
	int i;
	for (i = 0; i < sizeof(void*); i++) {
		ptr[i] = 0;
	}
}*/


